package com.zmn.plat.services.impl.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.user.User;
import com.zmn.plat.model.entity.user.UserQuery;
import com.zmn.plat.persistence.interfaces.user.UserDao;
import com.zmn.plat.services.interfaces.user.UserCountService;
import com.zmn.plat.services.interfaces.user.UserDeviceService;
import com.zmn.plat.services.interfaces.user.UserService;

/**
 * @author nowind
 * @since 17/07/31 14:11
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;
    @Resource
    private UserCountService userCountService;
    @Resource
    private UserDeviceService userDeviceService;

    @Override
    public User findByKey(Integer userId) {
        return userDao.findByKey(userId);
    }

    @Override
    public boolean findMobileExists(Long mobile, Integer userId) {

        User user = new User();
        user.setMobile(mobile);
        user.setUserId(userId);

        Integer isExists = userDao.findMobileExists(user);

        return isExists == null || isExists == 0 ? false : true;
    }

    @Override
    public boolean findMailExists(String mail, Integer userId) {

        User user = new User();
        user.setMail(mail);
        user.setUserId(userId);

        Integer isExists = userDao.findMailExists(user);

        return isExists == null || isExists == 0 ? false : true;
    }

    @Override
    public List<User> listPageByQuery(UserQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : userDao.listPageByQuery(query);
    }

    @Override
    public List<User> listByQuery(UserQuery query) {
        return userDao.listByQuery(query);
    }

    @Override
    public Integer countByQuery(UserQuery query) {
        return userDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(User user) {
        return userDao.updateByKey(user);
    }

    @Override
    public Integer updateStatus(Integer userId, Integer status) {
        User user = new User();
        user.setUserId(userId);
        user.setStatus(status);
        return this.updateStatus(user);
    }

    @Override
    public Integer updateStatus(User user) {
        return userDao.updateStatus(user);
    }

    @Override
    public Integer updateStatusBatch(List<Integer> userIds, Integer status) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(userIds);
        dto.setStatus(status);
        return this.updateStatusBatch(dto);
    }

    @Override
    public Integer updateStatusBatch(StatusDTO dto) {
        return userDao.updateStatusBatch(dto);
    }

    @Override
    public Integer updateUserName(User user) {
        if (user.getOperatorType() == GlobalDict.OPERATOR_TYPE_USER && user.getUpdateTime() == null) {
            user.setUpdateTime(DateUtil.getNow());
        }
        return userDao.updateUserName(user);
    }

    @Override
    public Integer updateAvatar(User user) {
        if (user.getOperatorType() == GlobalDict.OPERATOR_TYPE_USER && user.getUpdateTime() == null) {
            user.setUpdateTime(DateUtil.getNow());
        }
        return userDao.updateAvatar(user);
    }

    @Override
    public Integer updateMobile(User user) {
        if (user.getOperatorType() == GlobalDict.OPERATOR_TYPE_USER && user.getUpdateTime() == null) {
            user.setUpdateTime(DateUtil.getNow());
        }
        return userDao.updateMobile(user);
    }

    @Override
    public Integer updateWx(User user) {
        if (user.getOperatorType() == GlobalDict.OPERATOR_TYPE_USER && user.getUpdateTime() == null) {
            user.setUpdateTime(DateUtil.getNow());
        }
        return userDao.updateWx(user);
    }

    @Override
    public Integer updateGroup(User user) {
        return userDao.updateGroup(user);
    }

    @Override
    public Integer updateAuthMobile(User user) {
        return userDao.updateAuthMobile(user);
    }

    @Override
    public Integer updateAuthMail(User user) {
        return userDao.updateAuthMail(user);
    }

    @Override
    public Integer updateEditName(User user) {
        return userDao.updateEditName(user);
    }

    @Override
    public Integer updateFocusWx(User user) {
        return userDao.updateFocusWx(user);
    }

    @Override
    public Integer updatePassword(User user) {
        if (user.getUpdateTime() == null) {
            user.setUpdateTime(DateUtil.getNow());
        }
        return userDao.updatePassword(user);
    }

    @Override
    public Integer insert(User user) {

        if (user.getUpdateTime() == null) {
            user.setUpdateTime(DateUtil.getNow());
        }
        user.setRegistTime(user.getUpdateTime());

        return userDao.insert(user);
    }

    @Override
    public Integer deleteByKey(Integer userId) {
        return userDao.deleteByKey(userId);
    }

}
